package cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.casual;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.matter.MatterApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.common.Constant;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.casualrecord.SendCasualRecordActivity;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class CasualMainActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_casual)
    RecyclerView mRvCasual;
    @BindView(R.id.activity_add_handover)
    LinearLayout mActivityAddHandover;

    MenuItem mMenuItem;

    MatterApi mMatterApi;


    CasualAdapter mCasualAdapter;

    List<CasualNotice> mCasualNotices = new ArrayList<>();


    boolean refresh = true;



    @Override
    public int initContentView() {
        return R.layout.activity_casual_main;
    }

    @Override
    public void initInjector() {
        mMatterApi = new MatterApi();
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());


        mCasualAdapter = new CasualAdapter(mCasualNotices);
        mRvCasual.setLayoutManager(new LinearLayoutManager(mContext));
        mRvCasual.setAdapter(mCasualAdapter);


    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Constant.ACTIVITY_REQUEST_CODE && resultCode == Constant.ACTIVITY_RESPONSE_NEED_REFRESH) {
            refresh = true;
        }





    }


    @Override
    protected void onResume() {
        fetchCasual();
//        if (refresh) {
//            refresh = false;
//            fetchCasual();
//        }
        super.onResume();





    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mMenuItem = menu.add("随手拍");
        mMenuItem.setIcon(R.mipmap.ic_casual_plus);
        mMenuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item == mMenuItem) {

            startActivityForResult(new Intent(mContext, SendCasualRecordActivity.class), Constant.ACTIVITY_REQUEST_CODE);

        }
        return super.onOptionsItemSelected(item);
    }


    private void fetchCasual() {
        //
        mMatterApi.getErrorList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult<List<CasualNotice>>>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult<List<CasualNotice>> result) {
                        mCasualNotices.clear();
                        mCasualNotices.addAll(result.getResultMessage());
                        mCasualAdapter.notifyDataSetChanged();
                    }
                });
    }
}
